<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Xangdau extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {

		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'xangdau_model', 'xangdau');
		$this->load->helper('text');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
		$this->load->dbutil();
	}
	
	
	/*
	 * Bao cao tong hop hoat dong dao tao
	 * Ngay 22/12
	 */
	
	public function rp_daotao($duration=null)
	{
		
	}
	
	public function rp_lanhdao($duration=null)
	{
		
	}
	
	public function rp_thcat($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('bdate');
			$edate = $this->input->post('edate');
			$kind = $this->input->post('kind');
			if($bdate == '' || $edate == '')
			{
				$bdate = date("01/m/Y");
				$edate = date("d/m/Y");
			}
			if($kind == 1)
			{
				
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata	=	$this->xangdau->get_report_thcat($duration);
				
				$return = '';
				if($listdata)
				{
					$return = $this->generate_rp_thcat($listdata);
				}
				echo json_encode(array('return_data'=>$return));
			}
			elseif($kind == 2)
			{
				//echo '123'; exit();
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata	=	$this->xangdau->get_report_ld($duration);
				
				$return = '';
				if($listdata)
				{
					
					$return = $this->generate_rp_ld($listdata);
				}
				echo json_encode(array('return_data'=>$return));
			}
			elseif($kind == 3)
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata = $this->xangdau->get_report_dt($duration);
				
				$return = '';
				if($listdata)
				{
					$return = $this->generate_rp_dt($listdata);
				}
				echo json_encode(array('return_data'=>$return));
			}
			elseif($kind == 5)
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata = $this->xangdau->get_report_dtdau($duration);
				
				$return = '';
				if($listdata)
				{
					$return = $this->generate_rp_dt($listdata);
				}
				echo json_encode(array('return_data'=>$return));
			}
			else
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata = $this->xangdau->get_report_kh($duration);
				
				$return = '';
				if($listdata)
				{
					$return = $this->generate_rp_kh($listdata);
				}
				echo json_encode(array('return_data'=>$return));
			}
		}
		else {
			$data['ItemID'] = 'User';
			$data['title'] = 'Báo cáo tổng hợp';
			$data['description'] = 'Báo cáo tổng hợp cấp phát xăng phục vụ đào tạo lái xe';
			$data['listdata']	=	$this->xangdau->get_report_thcat($duration);
			
			$this->form_validation->set_rules('soluong', 'Số lượng', '');
			$this->form_validation->set_error_delimiters('', '<br />');
			
			//$this->_template['page']	= 'users/xangdau/rptonghopxe';
			$this->_template['page']	= 'users/xangdau/rpthcat';
			$this->system_library->load($this->_template['page'], $data);
		}
	}
	
	public function generate_rp_kh($listdata)
	{
		$html = '';
		$wcount = $listdata['wcount'];
		$bdate = $listdata['bdate'];
		$bdatet = str_replace('/', '-', $bdate);
		$bdateintotime = strtotime($bdatet);
		//var_dump($array);
		if($listdata)
		{
			//$temp = $item['chungtus'];
			$html = $html. "<br /><p><strong> Báo cáo tổng hợp bán lẻ từ ngày ".$listdata['bdate']." đến ngày ".$listdata['edate']." </strong></p><br/>";
			
			$html = $html. '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">';
			$html = $html. '<thead>
				<tr>
					<th>Ngày Tháng</th>
					<th>Số lượng</th>
					<th>Giá</th>
					<th>Thành tiền</th>					
					<th>Ghi chú</th>
				</tr>
			</thead>
			<tbody>';
			$stt = 1;						
			$kh = $listdata['kh'];
			$total = 0;
			$amount = 0;
			for($i =0; $i< $wcount; $i++)
			{
				$from = $bdateintotime + $i*7*24*60*60;
				$to = $from + 7*24*60*60;
				$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td>'.date("d/m",$from).' - '.date("d/m",$to).'</td>';
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
				$html = $html.  number_format($kh[$i]['total'],2,'.',',');
				$html = $html. '</td>';
				$html = $html. '<td></td>';
				$html = $html. '<td class="ta-right">'. number_format($kh[$i]['amount'],0,'.',',').'</td>';
				$html = $html. '<td></td>';
				$html = $html. '</tr>';
				$stt++;
				$total = $total + $kh[$i]['total'];
				$amount = $amount + $kh[$i]['amount'];
			}
			//$ps = $listdata['ps'];
			
				$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td id="name_item_'.$stt.'"><strong> Tổng</strong></td>';
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right"><strong>';
				$html = $html.  number_format($total,2,'.',',');
				$html = $html. '</strong></td>';
				$html = $html. '<td></td>';
				$html = $html. '<td class="ta-right"><strong>'. number_format($amount,0,'.',',').'</strong></td>';
				$html = $html. '<td></td>';
				$html = $html. '</tr>';
				
			$html = $html.'</tbody>
			
		</table>
		<br/>';
				
		}
		return $html;
	}
	
	public function generate_rp_dt($listdata)
	{
		$html = '';
		//var_dump($array);
		$wcount = $listdata['wcount'];
		$bdate = $listdata['bdate'];
		$bdatet = str_replace('/', '-', $bdate);
		$bdateintotime = strtotime($bdatet);
		if($listdata)
		{
			$html = $html. "<br /><p><strong> Báo cáo tổng hợp từ ngày ".$listdata['bdate']." đến ngày ".$listdata['edate']." </strong></p><br/> Xe tổng ôn </br>";
			
			$html = $html. '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">';
			$html = $html. '<thead>
				<tr>
					<th rowspan="2">STT</th>
					<th rowspan="2">Số xe</th>
					<th rowspan="2">Giáo viên quản lý</th>
					<th colspan="'.$listdata['wcount'].'"> <div align="center"> Nhiên liệu thực cấp (Lít)</div></th>
					<th rowspan="2">Tổng</th>	
					<th rowspan="2">Ghi chú</th>
				</tr>
				<tr>
					'; 
			for($i = 0; $i < $wcount; $i++)
			{
				$from = $bdateintotime + $i*7*24*60*60;
				$to = $from + 7*24*60*60;
				$html = $html.'<td> '.date("d/m",$from).' đến '.date("d/m",$to).' </td>';
			}
			$html = $html.
					'
				</tr>
			</thead>
			<tbody>';
			$stt = 1;						
			$dt = $listdata['dt'];
			$coltotal = array();
			// Khởi tạo mảng tổng
			for($i = 0; $i < $wcount; $i++)
			{
				$coltotal[$i] = 0;
			}
			foreach($dt as $item)
			{
				$xe = $item['xe'];
				$nls = $item['nl'];
				$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td>'.$stt.'</td>';
				$html = $html. '<td id="name_item_'.$stt.'"> '.$xe['name'].'</td>';
				$html = $html. '<td> Giáo viên quản lý</td>';				
				$rowtotal = 0;
				$j = 0;
				foreach($nls as $litem)
				{
					$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
					$html = $html.  number_format($litem,2,'.',',');
					$html = $html. '</td>';
					$rowtotal = $rowtotal + $litem;
					$coltotal[$j] = $coltotal[$j] + $litem;
					$j++;
				}
				$html = $html. '<td class="ta-right"><strong>'.number_format($rowtotal,2,'.',',').'</strong></td>';
	
				$html = $html. '</tr>';
				$stt++;
				//$totald = $totald + $item['dau'];
				//$totalx = $totalx + $item['xang'];
			}
			//$ps = $listdata['ps'];
			
			
			 	$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td></td>';
				$html = $html. '<td colspan="2" id="name_item_'.$stt.'"><strong> Tổng</strong></td>';
				$totalx = 0;
				foreach($coltotal as $litem)
				{
					$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right"><strong>';
					$html = $html.  number_format($litem,2,'.',',');
					$html = $html. '</strong></td>';
					$totalx = $totalx + $litem;
				}
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right"><strong>';
				$html = $html.  number_format($totalx,2,'.',',');
				$html = $html. '</strong></td>';
				$html = $html. '<td class="ta-right"></td>';
	
				$html = $html. '</tr>';
			
				
			$html = $html.'</tbody>
			
		</table>
		<br/>';
				
		}
		return $html;
	}
	
	public function xuat_rp_thcat($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('begin_date');
			$edate = $this->input->post('end_date');
			if($bdate == '' || $edate == '')
			{
				$listdata	=	$this->xangdau->get_report_tong_hop_xe();
				$filename = 'xuat_nhap_tong_hop'.date('d-m-Y');
				$title = 'Báo cáo tông hợp '.date('d-m-Y');
			}
			else
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$filename = 'xuat_nhap_tong hop_'.$bdate.'_den_ngay_'.$edate;
				$title ='Báo cáo tổng hợp từ ngày '.$bdate.' đến ngày '.$edate;
				$listdata	=	$this->xangdau->get_report_thcat($duration);
			}
			//$duration = $duration.'_'.$bdate.'_'.$edate;
			//$listdata	=	$this->xangdau->get_report_tong_hop_xe($duration);
			//var_dump($listdata);
			if($listdata)
			{
				$return = '';
				$return = $this->generate_excel_thcat($listdata,$filename,$title);
			}
			
		}
	}
	
	function generate_rp_ld($listdata)
	{
		$html = '';
		//var_dump($array);
		if($listdata)
		{
			//$temp = $item['chungtus'];
			$html = $html. "<br /><p><strong> Báo cáo tổng hợp từ ngày ".$listdata['bdate']." đến ngày ".$listdata['edate']." </strong></p><br/>";
			
			$html = $html. '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">';
			$html = $html. '<thead>
				<tr>
					<th>STT</th>
					<th>Số xe</th>
					<th>Giáo viên quản lý</th>
					<th>ĐVT</th>
					<th>Xăng</th>					
					<th>Dầu</th>
					<th>Ghi chú</th>
				</tr>
			</thead>
			<tbody>';
			$stt = 1;						
			$ld = $listdata['lanhdao'];
			$totalx = 0;
			$totald = 0;
			foreach($ld as $item)
			{
				$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td>'.$stt.'</td>';
				$html = $html. '<td id="name_item_'.$stt.'"> '.$item['name'].'</td>';
				$html = $html. '<td> Giáo viên quản lý</td>';
				$html = $html. '<td> ĐVT </td>';
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
				$html = $html.  number_format($item['xang'],2,'.',',');
				$html = $html. '</td>';
				$html = $html. '<td class="ta-right">'. number_format($item['dau'],0,'.',',').'</td>';
	
				$html = $html. '</tr>';
				$stt++;
				$totald = $totald + $item['dau'];
				$totalx = $totalx + $item['xang'];
			}
			//$ps = $listdata['ps'];
			
			$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td></td>';
				$html = $html. '<td id="name_item_'.$stt.'"><strong> Tổng</strong></td>';
				$html = $html. '<td></td>';
				$html = $html. '<td></td>';
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
				$html = $html.  number_format($totalx,2,'.',',');
				$html = $html. '</td>';
				$html = $html. '<td class="ta-right">'. number_format($totald,0,'.',',').'</td>';
	
				$html = $html. '</tr>';
				
			$html = $html.'</tbody>
			
		</table>
		<br/>';
				
		}
		return $html;
	}
	function generate_rp_thcat($listdata)
	{
		$html = '';
		//var_dump($array);
		if($listdata)
		{
			//$temp = $item['chungtus'];
			$html = $html. "<br /><p><strong> Báo cáo tổng hợp từ ngày ".$listdata['bdate']." đến ngày ".$listdata['edate']." </strong></p><br/>";
			
			$html = $html. '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">';
			$html = $html. '<thead>
				<tr>
					<th>STT</th>
					<th>Hạng mục</th>
					<th>Xăng</th>					
					<th>Dầu</th>
	
				</tr>
			</thead>
			<tbody>';
			$stt = 1;
			$sddk = $listdata['sddk'];
			$html = $html. '<tr id="item_'. $stt. '">';
			$html = $html. '<td>'.$stt.'</td>';
			$html = $html. '<td id="name_item_'.$stt.'"><strong> Số dư đầu kỳ</strong></td>';
			$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
			$html = $html.  number_format($sddk['xang'],2,'.',',');
			$html = $html. '</td>';
			$html = $html. '<td class="ta-right">'. number_format($sddk['dau'],0,'.',',').'</td>';
	
			$html = $html. '</tr>';
			$stt++;
			
			$ps = $listdata['ps'];
			$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td>'.$stt.'</td>';
				$html = $html. '<td id="name_item_'.$stt.'"><strong> Phát sinh tăng</strong></td>';
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
				$html = $html.  number_format($ps['xang'],2,'.',',');
				$html = $html. '</td>';
				$html = $html. '<td class="ta-right">'. number_format($ps['dau'],0,'.',',').'</td>';
	
				$html = $html. '</tr>';
			$stt++;
			$ld = $listdata['lanhdao'];
			$html = $html. '<tr id="item_'. $stt. '">';
			$html = $html. '<td>'.$stt.'</td>';
			$html = $html. '<td id="name_item_'.$stt.'"> Xe công vụ</td>';
			$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
			$html = $html.  number_format($ld['xang'],2,'.',',');
			$html = $html. '</td>';
			$html = $html. '<td class="ta-right">'. number_format($ld['dau'],0,'.',',').'</td>';
	
			$html = $html. '</tr>';
			$stt++;
			$dt = $listdata['daotao'];
			$html = $html. '<tr id="item_'. $stt. '">';
			$html = $html. '<td>'.$stt.'</td>';
			$html = $html. '<td id="name_item_'.$stt.'"> Xe đào tạo</td>';
			$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
			$html = $html.  number_format($dt['xang'],2,'.',',');
			$html = $html. '</td>';
			$html = $html. '<td class="ta-right">'. number_format($dt['dau'],0,'.',',').'</td>';
	
			$html = $html. '</tr>';
			
			$stt++;
			
			$kh = $listdata['khac'];
			$totalx = 0;
			$totald = 0;
			foreach($kh as $item)
			{
				$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td>'.$stt.'</td>';
				$html = $html. '<td id="name_item_'.$stt.'"> '.$item['name'].'</td>';
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
				$html = $html.  number_format($item['xang'],2,'.',',');
				$html = $html. '</td>';
				$html = $html. '<td class="ta-right">'. number_format($item['dau'],0,'.',',').'</td>';
	
				$html = $html. '</tr>';
				$stt++;
				$totald = $totald + $item['dau'];
				$totalx = $totalx + $item['xang'];
			}
			//$ps = $listdata['ps'];
			
			$ckx = $sddk['xang'] + $ps['xang'] - $totalx - $ld['xang'] - $dt['xang'];
			$ckd = $sddk['dau'] + $ps['dau'] - $totald - $ld['dau'] - $dt['dau'];
			$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td></td>';
				$html = $html. '<td id="name_item_'.$stt.'"><strong> Tổng số lượng nhiên liệu tiêu thụ</strong></td>';
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
				$html = $html.  number_format($totalx,2,'.',',');
				$html = $html. '</td>';
				$html = $html. '<td class="ta-right">'. number_format($totald,0,'.',',').'</td>';
	
				$html = $html. '</tr>';
				
			$stt++;

			$html = $html. '<tr id="item_'. $stt. '">';
				$html = $html. '<td></td>';
				$html = $html. '<td id="name_item_'.$stt.'"><strong> Số lượng tồn cuối kỳ</strong></td>';
				$html = $html. '<td id="ps_giam_'. $stt.'" class="ta-right">';
				$html = $html.  number_format($ckx,2,'.',',');
				$html = $html. '</td>';
				$html = $html. '<td class="ta-right">'. number_format($ckd,0,'.',',').'</td>';
	
				$html = $html. '</tr>';
			$html = $html.'</tbody>
			
		</table>
		<br/>';
				
		}
		return $html;
	}
	function generate_excel_thcat($listdata,$filename,$title='')
	{
		$arrT = array();
		$arrT[1] = 'A';
		$arrT[2] = 'B';
		$arrT[3] = 'C';
		$arrT[4] = 'D';
		$arrT[5] = 'E';
		$arrT[6] = 'F';
		$arrT[7] = 'G';
		$arrT[8] = 'H';
		$arrT[9] = 'I';
		$arrT[10] = 'J';
		$arrT[11] = 'K';
		$arrT[12] = 'L';
		$arrT[13] = 'M';
		$arrT[14] = 'N';
		$arrT[15] = 'O';
		$arrT[16] = 'P';
		$arrT[17] = 'Q';
		$arrT[18] = 'R';
		$arrT[19] = 'S';
		$arrT[20] = 'T';
		$arrT[21] = 'U';
		$arrT[22] = 'V';
		$arrT[23] = 'W';
		$arrT[24] = 'X';
		$arrT[25] = 'Y';
		$arrT[26] = 'Z';
		$arrT[27] = 'AA';
		$arrT[28] = 'AB';
		$arrT[29] = 'AC';
		$arrT[30] = 'AD';
		$arrT[31] = 'AE';
		$arrT[32] = 'AF';
		$arrT[33] = 'AG';
		$arrT[34] = 'AH';
		$arrT[35] = 'AI';
		$arrT[36] = 'AK';
		$arrT[37] = 'AL';
		
			
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
		$this->exzel->getActiveSheet()->setCellValue('A1', $title);
		
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		// Xử lý mảng
		$row = 6;
		foreach ($data as $item)
		{
			$temp = $item['chungtus'];
			$stt = 1;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, $item['name']);
			$row++;
			$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'TT');
			$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, 'Hạng mục');
			$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
			$this->exzel->getActiveSheet()->setCellValue('C'.$row, 'Nhận (Lít)');
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, 'Thành tiền');
			$this->exzel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
			$row++;
			$jtotal = $row;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'Nhà trường');
			$row++;
			//print data
			$total = 0;
			$amount = 0;
			foreach ($temp as $obj)
			{
				if($obj['tbname'] !='KH')
				{
					$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
					$this->exzel->getActiveSheet()->setCellValue('B'.$row, $obj['tbname']);
					$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($obj['total'],2,'.',','));
					$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($obj['amount'],0,'.',','));
					$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
					$total = $total + 	$obj['total'];
					$amount = $amount + $obj['amount'];
					$stt++;
					$row++;
				}
			}
			
			$this->exzel->getActiveSheet()->setCellValue('C'.$jtotal, number_format($total,2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('D'.$jtotal, number_format($amount,0,'.',','));
			$this->exzel->getActiveSheet()->getStyle('D'.$jtotal)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				
			$itotal = $row;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'Khách hàng lẻ');
			$row++;
			$khtotal =0;
			$khamount = 0;
			foreach ($temp as $obj)
			{
				if($obj['tbname'] =='KH')
				{
					$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
					$this->exzel->getActiveSheet()->setCellValue('B'.$row, $obj['tbname']);
					$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($obj['total'],2,'.',','));
					$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($obj['amount'],0,'.',','));
					$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
			
					$khtotal = 	$khtotal + $obj['total'];
					$khamount = $khamount + $obj['amount'];
					$stt++;
					$row++;
				}
			}
			$this->exzel->getActiveSheet()->setCellValue('C'.$itotal, number_format($khtotal,2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('D'.$itotal, number_format($khamount,0,'.',','));
			$this->exzel->getActiveSheet()->getStyle('D'.$itotal)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				
			$row++;
			//change the font size
			//$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setSize(14);
			//make the font become bold
			//$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setBold(false);
			//merge cell A1 until D1
			//$this->exzel->getActiveSheet()->mergeCells('A7:A9');
			//set aligment to center for that merged cell (A1 to D1)
			//$this->exzel->getActiveSheet()->getStyle('A7')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			//$this->exzel->getActiveSheet()->getStyle('E7')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			//$this->exzel->getActiveSheet()->mergeCells('B7:B9');
			//$this->exzel->getActiveSheet()->mergeCells('C7:C9');
			//$this->exzel->getActiveSheet()->mergeCells('D7:D9');
		}
				
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	/*
	 * Ket thuc bao tao 22/12
	 */
	// doit
	public function doit()
	{
		$rss = $this->xangdau->doit();
		$json = array();
		foreach($rss as $item)
		{
			$i = array();
			$i['id'] = $item['id'];
			$i['name'] = $item['name'];
			$i['code'] = $item['code'];
			$json[] = $i;
		}
		echo json_encode($json); 
	}
	
	public function xuat_report_xe($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('begin_date');
			$edate = $this->input->post('end_date');
			if($bdate == '' || $edate == '')
			{
				$listdata	=	$this->xangdau->get_report_tong_hop_xe();
				$filename = 'xuat_nhap_theo_xe_thang_hien_thoi'.date('d-m-Y');
				$title = 'Báo cáo tháng hiện thời '.date('d-m-Y');
			}
			else
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$filename = 'xuat_nhap_theo_xe_tu_'.$bdate.'_den_ngay_'.$edate;
				$title ='Báo cáo theo xe từ ngày '.$bdate.' đến ngày '.$edate;
				$listdata	=	$this->xangdau->get_report_tong_hop_xe($duration);
			}
			//$duration = $duration.'_'.$bdate.'_'.$edate;
			//$listdata	=	$this->xangdau->get_report_tong_hop_xe($duration);
			//var_dump($listdata);
			if($listdata)
			{
				$return = '';
				$return = $this->generate_excel($listdata,$filename,$title);
			}
			
		}
	}
	
	public function generate_excel($data,$filename,$title)
	{

		$arrT = array();
		$arrT[1] = 'A';
		$arrT[2] = 'B';
		$arrT[3] = 'C';
		$arrT[4] = 'D';
		$arrT[5] = 'E';
		$arrT[6] = 'F';
		$arrT[7] = 'G';
		$arrT[8] = 'H';
		$arrT[9] = 'I';
		$arrT[10] = 'J';
		$arrT[11] = 'K';
		$arrT[12] = 'L';
		$arrT[13] = 'M';
		$arrT[14] = 'N';
		$arrT[15] = 'O';
		$arrT[16] = 'P';
		$arrT[17] = 'Q';
		$arrT[18] = 'R';
		$arrT[19] = 'S';
		$arrT[20] = 'T';
		$arrT[21] = 'U';
		$arrT[22] = 'V';
		$arrT[23] = 'W';
		$arrT[24] = 'X';
		$arrT[25] = 'Y';
		$arrT[26] = 'Z';
		$arrT[27] = 'AA';
		$arrT[28] = 'AB';
		$arrT[29] = 'AC';
		$arrT[30] = 'AD';
		$arrT[31] = 'AE';
		$arrT[32] = 'AF';
		$arrT[33] = 'AG';
		$arrT[34] = 'AH';
		$arrT[35] = 'AI';
		$arrT[36] = 'AK';
		$arrT[37] = 'AL';
		
			
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
		$this->exzel->getActiveSheet()->setCellValue('A1', $title);
		
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		// Xử lý mảng
		$row = 6;
		foreach ($data as $item)
		{
			$temp = $item['chungtus'];
			$stt = 1;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, $item['name']);
			$row++;
			$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'TT');
			$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, 'Hạng mục');
			$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
			$this->exzel->getActiveSheet()->setCellValue('C'.$row, 'Nhận (Lít)');
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, 'Thành tiền');
			$this->exzel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
			$row++;
			$jtotal = $row;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'Nhà trường');
			$row++;
			//print data
			$total = 0;
			$amount = 0;
			foreach ($temp as $obj)
			{
				if($obj['tbname'] !='KH')
				{
					$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
					$this->exzel->getActiveSheet()->setCellValue('B'.$row, $obj['tbname']);
					$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($obj['total'],2,'.',','));
					$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($obj['amount'],0,'.',','));
					$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
					$total = $total + 	$obj['total'];
					$amount = $amount + $obj['amount'];
					$stt++;
					$row++;
				}
			}
			
			$this->exzel->getActiveSheet()->setCellValue('C'.$jtotal, number_format($total,2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('D'.$jtotal, number_format($amount,0,'.',','));
			$this->exzel->getActiveSheet()->getStyle('D'.$jtotal)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				
			$itotal = $row;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'Khách hàng lẻ');
			$row++;
			$khtotal =0;
			$khamount = 0;
			foreach ($temp as $obj)
			{
				if($obj['tbname'] =='KH')
				{
					$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
					$this->exzel->getActiveSheet()->setCellValue('B'.$row, $obj['tbname']);
					$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($obj['total'],2,'.',','));
					$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($obj['amount'],0,'.',','));
					$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
			
					$khtotal = 	$khtotal + $obj['total'];
					$khamount = $khamount + $obj['amount'];
					$stt++;
					$row++;
				}
			}
			$this->exzel->getActiveSheet()->setCellValue('C'.$itotal, number_format($khtotal,2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('D'.$itotal, number_format($khamount,0,'.',','));
			$this->exzel->getActiveSheet()->getStyle('D'.$itotal)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				
			$row++;
			//change the font size
			//$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setSize(14);
			//make the font become bold
			//$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setBold(false);
			//merge cell A1 until D1
			//$this->exzel->getActiveSheet()->mergeCells('A7:A9');
			//set aligment to center for that merged cell (A1 to D1)
			//$this->exzel->getActiveSheet()->getStyle('A7')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			//$this->exzel->getActiveSheet()->getStyle('E7')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			//$this->exzel->getActiveSheet()->mergeCells('B7:B9');
			//$this->exzel->getActiveSheet()->mergeCells('C7:C9');
			//$this->exzel->getActiveSheet()->mergeCells('D7:D9');
		}
				
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	// Report chi tiết theo xe (theo từng xe
	public function report_detail_by_theitem($itemId=null,$duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('bdate');
			$edate = $this->input->post('edate');
			if($bdate == '' || $edate == '')
			{
				$listdata	=	$this->xangdau->get_report_chi_tiet_xe($itemId);
			}
			else
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata	=	$this->xangdau->get_report_chi_tiet_xe($itemId,$duration);
			}
			$return = '';
			if($listdata)
			{
					$return = $this->generate_report_chi_tiet_xe($listdata);
			}
			echo json_encode(array('return_data'=>$return));
		}
		else {
			$data['ItemID'] = 'User';
			$data['title'] = 'Báo cáo tổng hợp theo xe';
			$data['description'] = 'Báo cáo tổng hợp theo xe';
			$data['listdata']	=	$this->xangdau->get_report_chi_tiet_xe($itemId);
			
			$this->form_validation->set_rules('soluong', 'Số lượng', '');
			$this->form_validation->set_error_delimiters('', '<br />');
			
			$this->_template['page']	= 'users/xangdau/rpchitietxe';
			$this->system_library->load($this->_template['page'], $data);
		}
	}
	public function generate_report_chi_tiet_xe($listdata)
	{
		$html = '';
		//var_dump($array);
		if($listdata != null)
		{
			$html = '<p><strong>'. $listdata[0]['tbname'].'</strong></p>';
			$html = $html . '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">';
			$html = $html . '<thead>
					<tr>
						<th>STT</th>
						<th>Ngày tháng</th>
						<th>Nhận (Lít)</th>
						<th>Giá</th>
						<th>Nhận (Đ)</th>
		
					</tr>
				</thead>';
				$html = $html . '<tbody>';
				$stt = 1;
				$sum = 0; 
				$total = 0;
				foreach($listdata as $data):
					$html = $html . '<tr id="item_'. $data['id'].'">';
					$html = $html . '<td>'. $stt .'</td>';
					$stt++;
					$html = $html . '<td id="name_item_'. $data['id'].'">'. date('d/m/Y',strtotime($data['created_date'])).'</td>';
					$html = $html . '<td id="ps_giam_'. $data['id'].'" class="ta-right">';
					$html = $html . number_format($data['ps_giam'],2,'.',',').'</td>';
					$html = $html . '<td class="ta-right">'.number_format($data['price'],0,'.',',').'</td>';
					$sum = $sum + $data['ps_giam']*$data['price']; 
					$total = $total + $data['ps_giam'];
					
					$html = $html . '<td class="ta-right">'. number_format($data['ps_giam']*$data['price'],0,'.',',').'</td>';
					$html = $html . '</tr>';				
				endforeach;
					$html = $html . '<tr>
					<td>
						Tổng
					</td>
					<td>
					</td>
					<td class="ta-right">';
					$html = $html . '<strong>'. number_format($total,2,'.',',').'</strong>
					</td>
					<td></td>
					<td class="ta-right">
						<strong>'. number_format($sum,0,'.',',').'</strong>
					</td>
					</tr>
				</tbody>
				
			</table>
			<br/>';
		}
		return $html;
	}
	
	public function download($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('bdate');
			$edate = $this->input->post('edate');
			$kind = $this->input->post('kind');
			if($bdate == '' || $edate == '')
			{
				//Lây dữ liệu tháng hiện tại
				$bdate = date("01/m/Y");
				$edate = date("d/m/Y");
			}
			  // Lấy dữ liệu từ ngày này đến ngày khác
			$this->downloadreportc($kind,$bdate,$edate);
			
			
		}
	}
	
	private function downloadreportc($kind,$bdate,$edate)
	{
		switch($kind)
		{
			case 1:
				$this->downloadreporctth($bdate,$edate);
			case 2:
				$this->downloadreportcxecongvu($bdate,$edate);
			case 3:
				$this->downloadreportcxedaotao($bdate,$edate);
			case 5:
				$this->downloadreportcxedaotaodau($bdate,$edate);
			case 4:
				$this->downloadreportcbanle($bdate,$edate);
		}
			
	}
	
	private function downloadreporctth($bdate,$edate)
	{
		// Lấy dữ liệu
		$duration = "CUSTOM_".$bdate."_".$edate;
		$listdata	=	$this->xangdau->get_report_thcat($duration);
		
		$filename = "bao-cao-tong-hop";
		$title1 ="BÁO CÁO TỔNG HỢP";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày ".$bdate." đến ngày ".$listdata['edate']." năm)";
		
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(35);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:E1');
		$this->exzel->getActiveSheet()->mergeCells('C2:E2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C4', $strdate);
		
		$this->exzel->getActiveSheet()->setCellValue('B5', $title2);
		
		
		$ld = $listdata['lanhdao'];
		$dt = $listdata['daotao'];
		$kh = $listdata['khac'];
		$sddk = $listdata['sddk'];
		$ps = $listdata['ps'];
			//	echo $duration; exit();
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Xăng(lít)");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Dầu(Lít)");
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "Ghi chú");
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
		
		$row++;
		
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "1");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số lượng tồn báo cáo trước");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($sddk['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($sddk['dau'],2,'.',','));
		
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		// Xe lãnh đạo
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "2");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Nhập kho - Phát sinh tăng");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($ps['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($ps['dau'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
	
		
		$row++;
		// Xe lãnh đạo
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "3");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Cấp xe lãnh đạo");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($ld['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($ld['dau'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		// Xe Huấn luyện
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "4");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Cấp xe huấn luyện");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($dt['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($dt['dau'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		$stt = 4;
		$totalx = $ld['xang'] + $dt['xang'];
		$totald = $ld['dau'] + $dt['dau'];
		// Khác
		foreach($kh  as $item)
		{
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['name']);
			$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($item['xang'],2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($item['dau'],2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
			$totalx = $totalx + $item['xang'];
			$totald = $totald + $item['dau'];
			$stt++;
			$row++;
		}
		
		$ckx = $sddk['xang'] + $ps['xang'] - $totalx;
		$ckd = $sddk['dau'] + $ps['dau'] - $totald;
		// Cắt cỏ hành chính
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tổng số lượng nhiên liệu tiêu thụ");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($totalx,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($totald,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số lượng tồn");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($ckx,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($ckd,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Tổng tiền tồn hết ngày ".$bdate);
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Ngày .... nộp cho Đ/c Trường");
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Tổng lượng xăng bán được từ ngày ".$bdate." đến ".$edate);
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Dầu, mỡ phụ mua trong tháng");
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Tổng tiền tồn hết ngày ".$edate." là:");
		
		$row++;
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "P/T. TRƯỞNG BAN XĂNG DẦU");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "NGƯỜI LẬP");
		
		
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	
	}
	
	private function downloadreportcxecongvu($bdate,$edate)
	{
		//$duration = "CUSTOM_".$bdate."_".$edate;
		// Lấy dữ liệu
		$duration = "CUSTOM_".$bdate."_".$edate;
		$listdata	=	$this->xangdau->get_report_ld($duration);
		
		$filename = "bao-cao-xe-cong-vu-lanh-dao";
		$title1 ="BÁO CÁO TỔNG HỢP";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày ".$bdate." đến ngày ".$listdata['edate']." năm)";
		
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(35);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:E1');
		$this->exzel->getActiveSheet()->mergeCells('C2:E2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C4', $strdate);
		
		$this->exzel->getActiveSheet()->setCellValue('B5', $title2);
		
		
		
		$ld = $listdata['lanhdao'];
		//$dt = $listdata['daotao'];
		//$kh = $listdata['khac'];
		//$sddk = $listdata['sddk'];
		//$ps = $listdata['ps'];
			//	echo $duration; exit();
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số xe");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Giao viên quản lý");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "ĐVT");
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "XăngA92");
		$this->exzel->getActiveSheet()->setCellValue('F'.$row, "Dầu");
		$this->exzel->getActiveSheet()->setCellValue('G'.$row, "Ghi chú");
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
				$this->exzel->getActiveSheet()->getStyle('F'.$row)->getFont()->setBold(true);
						$this->exzel->getActiveSheet()->getStyle('G'.$row)->getFont()->setBold(true);
		$row++;
		$stt = 1;
		$totalx = 0;
		$totald = 0;
		// Khác
		foreach($ld  as $item)
		{
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['name']);
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Lít");
			$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($item['xang'],2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('F'.$row, number_format($item['dau'],2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('G'.$row, "");
			$totalx = $totalx + $item['xang'];
			$totald = $totald + $item['dau'];
			$stt++;
			$row++;
		}
		
		// Cắt cỏ hành chính
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tổng");
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($totalx,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('F'.$row, number_format($totald,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('G'.$row, "");
		
		$row++;
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "P/T. TRƯỞNG BAN XĂNG DẦU");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "NGƯỜI LẬP");
		
		
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	
	}
	
	private function downloadreportcxedaotaodau($bdate,$edate)
	{
		//$duration = "CUSTOM_".$bdate."_".$edate;
		// Lấy dữ liệu
		$duration = "CUSTOM_".$bdate."_".$edate;
		$listdata = $this->xangdau->get_report_dtdau($duration);
		$wcount = $listdata['wcount'];
		$bdate = $listdata['bdate'];
		$bdatet = str_replace('/', '-', $bdate);
		$bdateintotime = strtotime($bdatet);
		$arrT = array();
		$arrT[1] = 'A';
		$arrT[2] = 'B';
		$arrT[3] = 'C';
		$arrT[4] = 'D';
		$arrT[5] = 'E';
		$arrT[6] = 'F';
		$arrT[7] = 'G';
		$arrT[8] = 'H';
		$arrT[9] = 'I';
		$arrT[10] = 'J';
		$arrT[11] = 'K';
		$arrT[12] = 'L';
		$arrT[13] = 'M';
		$arrT[14] = 'N';
		$arrT[15] = 'O';
		$arrT[16] = 'P';
		$arrT[17] = 'Q';
		$arrT[18] = 'R';
		$arrT[19] = 'S';
		$arrT[20] = 'T';
		$arrT[21] = 'U';
		$arrT[22] = 'V';
		$arrT[23] = 'W';
		$arrT[24] = 'X';
		$arrT[25] = 'Y';
		$arrT[26] = 'Z';
		$arrT[27] = 'AA';
		$arrT[28] = 'AB';
		$arrT[29] = 'AC';
		$arrT[30] = 'AD';
		$arrT[31] = 'AE';
		$arrT[32] = 'AF';
		$arrT[33] = 'AG';
		$arrT[34] = 'AH';
		$arrT[35] = 'AI';
		$arrT[36] = 'AJ';
		$arrT[37] = 'AK';
		$arrT[38] = 'AL';
		$arrT[39] = 'AM';
		$arrT[40] = 'AN';
		$arrT[41] = 'AO';
		$arrT[42] = 'AP';
		$arrT[43] = 'AQ';
		$arrT[44] = 'AR';
		$arrT[45] = 'AS';
		$arrT[46] = 'AT';
		$arrT[47] = 'AU';
		$arrT[48] = 'AV';
		$arrT[49] = 'AW';
		$arrT[50] = 'AX';
		$arrT[51] = 'AY';
		$filename = "bao-cao-xe-dao-tao-dau";
		$title1 ="BÁO CÁO TỔNG HỢP";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày ".$bdate." đến ngày ".$listdata['edate']." năm)";
		
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(35);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:E1');
		$this->exzel->getActiveSheet()->mergeCells('C2:E2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C4', $strdate);
		
		$this->exzel->getActiveSheet()->setCellValue('B5', $title2);
		
		
		
		$dt = $listdata['dt'];
		//$dt = $listdata['daotao'];
		//$kh = $listdata['khac'];
		//$sddk = $listdata['sddk'];
		//$ps = $listdata['ps'];
			//	echo $duration; exit();
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu
		$this->exzel->getActiveSheet()->mergeCells('A8:A9');
		$this->exzel->getActiveSheet()->mergeCells('B8:B9');
		$this->exzel->getActiveSheet()->mergeCells('C8:C9');
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số xe");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Giao viên quản lý");
		$colnum = 3+$wcount;
		$this->exzel->getActiveSheet()->mergeCells('D'.$row.':'.$arrT[$colnum].$row);
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Nhiên liệu thực cấp");
		$colnum = $colnum+1;
		$this->exzel->getActiveSheet()->mergeCells($arrT[$colnum].'8:'.$arrT[$colnum].'9');
		$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, "Tổng cộng");
		$colnum = $colnum+1;
		$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, "Ghi chú");
		$this->exzel->getActiveSheet()->mergeCells($arrT[$colnum].'8:'.$arrT[$colnum].'9');
		
		
		
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
				$this->exzel->getActiveSheet()->getStyle('F'.$row)->getFont()->setBold(true);
						$this->exzel->getActiveSheet()->getStyle('G'.$row)->getFont()->setBold(true);
		
		$colnum = 4;
		for($i = 0; $i < $wcount; $i++)
		{
			
			$from = $bdateintotime + $i*7*24*60*60;
			$to = $from + 7*24*60*60;
			$strtmp = date("d/m",$from)." đến ".date("d/m",$to);
			$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].'9', $strtmp);
			$colnum++;
			//$html = $html.'<td> '.date("d/m",$from).' đến '.date("d/m",$to).' </td>';
		}
						
		$row = 10;
		$stt = 1;
		$colnum = 3;
		
		$totalc = array();
		for($i = 0; $i < $wcount; $i++)
		{
			$totalc[$i] = 0;
		}
		// Khác
		foreach($dt  as $item)
		{
			$xe = $item['xe'];
			$nls = $item['nl'];
			$colnum = 4;
			$totalr = 0;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, $xe['name']);
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, "");
			$j=0;
			foreach($nls as $litem)
			{
				
				$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, number_format($litem,2,'.',','));
				$totalr = $totalr + $litem;
				$totalc[$j] = $totalc[$j] + $litem;
				$colnum++;
				$j++;
			}	
			$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, number_format($totalr,2,'.',','));		
			$stt++;
			$row++;
		}
		//var_dump($totalc);
		// Cắt cỏ hành chính
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tổng");
		// In Tổng cột
		$colnum = 3;
		$total = 0;
		for($i = 0; $i < $wcount; $i++)
		{
			$colnum++;
			$total = $total + $totalc[$i];
			$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, number_format($totalc[$i],2,'.',','));
		}
		$colnum++;
		$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, number_format($total,2,'.',','));
	
		// in tổng hàng cột thứ 3 + $wcount + 1;
		
		
		
		$row++;
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "P/T. TRƯỞNG BAN XĂNG DẦU");
		$this->exzel->getActiveSheet()->setCellValue('G'.$row, "NGƯỜI LẬP");
		
		
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	
	private function downloadreportcxedaotao($bdate,$edate)
	{
		//$duration = "CUSTOM_".$bdate."_".$edate;
		// Lấy dữ liệu
		$duration = "CUSTOM_".$bdate."_".$edate;
		$listdata = $this->xangdau->get_report_dt($duration);
		$wcount = $listdata['wcount'];
		$bdate = $listdata['bdate'];
		$bdatet = str_replace('/', '-', $bdate);
		$bdateintotime = strtotime($bdatet);
		$arrT = array();
		$arrT[1] = 'A';
		$arrT[2] = 'B';
		$arrT[3] = 'C';
		$arrT[4] = 'D';
		$arrT[5] = 'E';
		$arrT[6] = 'F';
		$arrT[7] = 'G';
		$arrT[8] = 'H';
		$arrT[9] = 'I';
		$arrT[10] = 'J';
		$arrT[11] = 'K';
		$arrT[12] = 'L';
		$arrT[13] = 'M';
		$arrT[14] = 'N';
		$arrT[15] = 'O';
		$arrT[16] = 'P';
		$arrT[17] = 'Q';
		$arrT[18] = 'R';
		$arrT[19] = 'S';
		$arrT[20] = 'T';
		$arrT[21] = 'U';
		$arrT[22] = 'V';
		$arrT[23] = 'W';
		$arrT[24] = 'X';
		$arrT[25] = 'Y';
		$arrT[26] = 'Z';
		$arrT[27] = 'AA';
		$arrT[28] = 'AB';
		$arrT[29] = 'AC';
		$arrT[30] = 'AD';
		$arrT[31] = 'AE';
		$arrT[32] = 'AF';
		$arrT[33] = 'AG';
		$arrT[34] = 'AH';
		$arrT[35] = 'AI';
		$arrT[36] = 'AJ';
		$arrT[37] = 'AK';
		$arrT[38] = 'AL';
		$arrT[39] = 'AM';
		$arrT[40] = 'AN';
		$arrT[41] = 'AO';
		$arrT[42] = 'AP';
		$arrT[43] = 'AQ';
		$arrT[44] = 'AR';
		$arrT[45] = 'AS';
		$arrT[46] = 'AT';
		$arrT[47] = 'AU';
		$arrT[48] = 'AV';
		$arrT[49] = 'AW';
		$arrT[50] = 'AX';
		$arrT[51] = 'AY';
		$filename = "bao-cao-xe-dao-tao-xang";
		$title1 ="BÁO CÁO TỔNG HỢP";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày ".$bdate." đến ngày ".$listdata['edate']." năm)";
		
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(35);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:E1');
		$this->exzel->getActiveSheet()->mergeCells('C2:E2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C4', $strdate);
		
		$this->exzel->getActiveSheet()->setCellValue('B5', $title2);
		
		
		
		$dt = $listdata['dt'];
		//$dt = $listdata['daotao'];
		//$kh = $listdata['khac'];
		//$sddk = $listdata['sddk'];
		//$ps = $listdata['ps'];
			//	echo $duration; exit();
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu
		$this->exzel->getActiveSheet()->mergeCells('A8:A9');
		$this->exzel->getActiveSheet()->mergeCells('B8:B9');
		$this->exzel->getActiveSheet()->mergeCells('C8:C9');
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số xe");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Giao viên quản lý");
		$colnum = 3+$wcount;
		$this->exzel->getActiveSheet()->mergeCells('D'.$row.':'.$arrT[$colnum].$row);
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Nhiên liệu thực cấp");
		$colnum = $colnum+1;
		$this->exzel->getActiveSheet()->mergeCells($arrT[$colnum].'8:'.$arrT[$colnum].'9');
		$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, "Tổng cộng");
		$colnum = $colnum+1;
		$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, "Ghi chú");
		$this->exzel->getActiveSheet()->mergeCells($arrT[$colnum].'8:'.$arrT[$colnum].'9');
		
		
		
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
				$this->exzel->getActiveSheet()->getStyle('F'.$row)->getFont()->setBold(true);
						$this->exzel->getActiveSheet()->getStyle('G'.$row)->getFont()->setBold(true);
		
		$colnum = 4;
		for($i = 0; $i < $wcount; $i++)
		{
			
			$from = $bdateintotime + $i*7*24*60*60;
			$to = $from + 7*24*60*60;
			$strtmp = date("d/m",$from)." đến ".date("d/m",$to);
			$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].'9', $strtmp);
			$colnum++;
			//$html = $html.'<td> '.date("d/m",$from).' đến '.date("d/m",$to).' </td>';
		}
						
		$row = 10;
		$stt = 1;
		$colnum = 3;
		
		$totalc = array();
		for($i = 0; $i < $wcount; $i++)
		{
			$totalc[$i] = 0;
		}
		// Khác
		foreach($dt  as $item)
		{
			$xe = $item['xe'];
			$nls = $item['nl'];
			$colnum = 4;
			$totalr = 0;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, $xe['name']);
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, "");
			$j=0;
			foreach($nls as $litem)
			{
				
				$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, number_format($litem,2,'.',','));
				$totalr = $totalr + $litem;
				$totalc[$j] = $totalc[$j] + $litem;
				$colnum++;
				$j++;
			}	
			$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, number_format($totalr,2,'.',','));		
			$stt++;
			$row++;
		}
		//var_dump($totalc);
		// Cắt cỏ hành chính
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tổng");
		// In Tổng cột
		$colnum = 3;
		$total = 0;
		for($i = 0; $i < $wcount; $i++)
		{
			$colnum++;
			$total = $total + $totalc[$i];
			$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, number_format($totalc[$i],2,'.',','));
		}
		$colnum++;
		$this->exzel->getActiveSheet()->setCellValue($arrT[$colnum].$row, number_format($total,2,'.',','));
	
		// in tổng hàng cột thứ 3 + $wcount + 1;
		
		
		
		$row++;
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "P/T. TRƯỞNG BAN XĂNG DẦU");
		$this->exzel->getActiveSheet()->setCellValue('G'.$row, "NGƯỜI LẬP");
		
		
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	
	private function downloadreportcbanle($bdate,$edate)
	{
		//$duration = "CUSTOM_".$bdate."_".$edate;
		// Lấy dữ liệu
		$duration = "CUSTOM_".$bdate."_".$edate;
		$listdata = $this->xangdau->get_report_kh($duration);
		$wcount = $listdata['wcount'];
		$bdate = $listdata['bdate'];
		$bdatet = str_replace('/', '-', $bdate);
		$bdateintotime = strtotime($bdatet);
		
		$filename = "bao-cao-xe-dao-tao-xang";
		$title1 ="BÁO CÁO TỔNG HỢP";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "Bảng tổng hợp bán lẻ xăng, dầu (từ ngày ".$bdate." đến ngày ".$listdata['edate']." năm)";
		
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(35);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:E1');
		$this->exzel->getActiveSheet()->mergeCells('C2:E2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C4', $strdate);
		
		$this->exzel->getActiveSheet()->setCellValue('B5', $title2);
		
		
		$kh = $listdata['kh'];
		$total = 0;
		$amount = 0;
			
		//$dt = $listdata['daotao'];
		//$kh = $listdata['khac'];
		//$sddk = $listdata['sddk'];
		//$ps = $listdata['ps'];
			//	echo $duration; exit();
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu
		
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Ngày Tháng");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số lượng (lít)");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Đơn giá");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Thành tiền");
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "Ghi chú");
				
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
		
		$row++;		
		$this->exzel->getActiveSheet()->mergeCells('A'.$row.':E'.$row);
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "XĂNG");				
		$row++;
		// Print
		for($i =0; $i< $wcount; $i++)
		{
				$from = $bdateintotime + $i*7*24*60*60;
				$to = $from + 7*24*60*60;
				$strtmp = date("d/m",$from).' - '.date("d/m",$to);
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, $strtmp);
				$this->exzel->getActiveSheet()->getStyle('D'.$row)->getNumberFormat()->setFormatCode("#,##0");
		$this->exzel->getActiveSheet()->getStyle('B'.$row)->getNumberFormat()->setFormatCode("#,##0");
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $kh[$i]['total']);
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, $kh[$i]['amount']);
				$total = $total + $kh[$i]['total'];
				$amount = $amount + $kh[$i]['amount'];
				$row++;
		}
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getNumberFormat()->setFormatCode("#,##0");
		$this->exzel->getActiveSheet()->getStyle('B'.$row)->getNumberFormat()->setFormatCode("#,##0");
		
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, $total);
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, $amount);
				
		
		
		$row++;
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "P/T. TRƯỞNG BAN XĂNG DẦU");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "NGƯỜI LẬP");
		
		
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	private function downloadreport($kind)
	{
		switch($kind)
		{
			case 1:
				$this->downloadreportth();
			case 2:
				$this->downloadreportxecongvu();
			case 3:
				$this->downloadreportthxedaotao();
			case 4:
				$this->downloadreportbanle();
		}
			
	}
	
	private function downloadreportth()
	{
		$filename = "bao-cao-tong-hop";
		$title1 ="BÁO CÁO TỔNG HỢP";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày.... đến ngày .... năm)";
		
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(35);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:E1');
		$this->exzel->getActiveSheet()->mergeCells('C2:E2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C4', $strdate);
		
		$this->exzel->getActiveSheet()->setCellValue('C4', $strdate);
		
		
		// Lấy dữ liệu
		$listdata	=	$this->xangdau->get_report_thcat();
		
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày.... đến ngày .... năm)";
		
		$xelanhdao = $this->xangdau->get_ps_xelanhdao();
		
		$xehuanluyen = $this->xangdau->get_ps_xehuanluyen();
		
		$banle =$this->xangdau->get_ps_banle();
		$capxangcatcohc =$this->xangdau->get_ps_catcohc();
		$capxangcatcokhoaxe =$this->xangdau->get_ps_catcokhoaxe();
		
		
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Xăng(lít)");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Dầu(Lít)");
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "Ghi chú");
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
		
		$row++;
		
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "1");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số lượng tồn báo cáo trước");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($listdata['0']['sddk'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($listdata['1']['sddk'],2,'.',','));
		
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		// Xe lãnh đạo
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "2");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Cấp xe lãnh đạo");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($xelanhdao['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($xelanhdao['dau'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		// Xe Huấn luyện
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "3");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Cấp xe huấn luyện");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($xehuanluyen['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($xehuanluyen['dau'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		// Bán lẻ
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "4");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Bán lẻ");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($banle['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($banle['dau'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		
		$row++;
		// Cắt cỏ hành chính
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "5");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Cấp xe cắt cỏ hành chính");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($capxangcatcohc['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($capxangcatcohc['dau'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "6");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Cấp xe cắt cỏ khoa xe");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($capxangcatcokhoaxe['xang'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($capxangcatcokhoaxe['dau'],2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$xtotal = $xelanhdao['xang'] + $xehuanluyen['xang'] + $banle['xang']+$capxangcatcohc['xang']+$capxangcatcokhoaxe['xang'];
		$dtotal =$xelanhdao['xang'] + $xehuanluyen['dau'] + $banle['dau']+$capxangcatcohc['dau']+$capxangcatcokhoaxe['dau'];
		
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "7");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tổng lượng nhiên liệu tiêu thụ");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($xtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($dtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số lượng tồn");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($listdata['0']['sddk']-$xtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($listdata['1']['sddk']-$dtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
		
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	private function downloadreportxecongvu()
	{
		$filename = "bao-cao-theo-xe-cong-vu";
		$title1 ="BÁO CÁO";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày.... đến ngày .... năm)";
		
			
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(25);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:F1');
		$this->exzel->getActiveSheet()->mergeCells('C2:F2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		
		// Lấy dữ liệu
		$xelanhdao = $this->xangdau->get_ps_theo_xelanhdao();
		
		
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu
		$this->exzel->getActiveSheet()->mergeCells('A8:A9');
		$this->exzel->getActiveSheet()->mergeCells('B8:B9');
		$this->exzel->getActiveSheet()->mergeCells('C8:C9');
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số xe");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Giảng viên");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "ĐVT");
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->mergeCells('E8:F8');
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "Số lượng nhiên liệu thực cấp");
		$this->exzel->getActiveSheet()->setCellValue('E9', "Xăng");
		$this->exzel->getActiveSheet()->setCellValue('F9', "Dầu Diesel");
		$row++;$stt=1;
		$row++;
		$xtotal =0;
		if($xelanhdao['xang'])
		{
			foreach($xelanhdao['xang'] as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['name']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, "");
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Lít");
				$this->exzel->getActiveSheet()->setCellValue('E'.$row, $item['total']);
				$xtotal = $xtotal + $item['total'];
				$row++;
				$stt++;
			}
		}
		$dtotal = 0;
		if($xelanhdao['dau'])
		{
			foreach($xelanhdao['dau'] as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['name']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, "");
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Lít");
				$this->exzel->getActiveSheet()->setCellValue('F'.$row, $item['total']);
				$dtotal = $dtotal + $item['total'];
				$row++;
				$stt++;
			}
		}
		
		//$xtotal = '0';//$xelanhdao['xang'] + $xehuanluyen['xang'] + $banle['xang']+$capxangcatcohc['xang']+$capxangcatcokhoaxe['xang'];
		//$dtotal ='0';//$xelanhdao['xang'] + $xehuanluyen['dau'] + $banle['dau']+$capxangcatcohc['dau']+$capxangcatcokhoaxe['dau'];
		
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "*");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tổng");
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($xtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('F'.$row, number_format($dtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('G'.$row, "");
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	private function downloadreportthxedaotao()
	{
		$filename = "bao-cao-theo-xe-dao-tao";
		$title1 ="BÁO CÁO";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày.... đến ngày .... năm)";
		
			
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
		$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(25);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:F1');
		$this->exzel->getActiveSheet()->mergeCells('C2:F2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		
		// Lấy dữ liệu
		$xelanhdao = $this->xangdau->get_ps_theo_xe_dao_tao();
		
		
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu
		$this->exzel->getActiveSheet()->mergeCells('A8:A9');
		$this->exzel->getActiveSheet()->mergeCells('B8:B9');
		$this->exzel->getActiveSheet()->mergeCells('C8:C9');
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số xe");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Giảng viên");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "ĐVT");
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->mergeCells('E8:F8');
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "Số lượng nhiên liệu thực cấp");
		$this->exzel->getActiveSheet()->setCellValue('E9', "Xăng");
		$this->exzel->getActiveSheet()->setCellValue('F9', "Dầu Diesel");
		$row++;$stt=1;
		$row++;
		$xtotal =0;
		if($xelanhdao['xang'])
		{
			foreach($xelanhdao['xang'] as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['name']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, "");
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Lít");
				$this->exzel->getActiveSheet()->setCellValue('E'.$row, $item['total']);
				$xtotal = $xtotal + $item['total'];
				$row++;
				$stt++;
			}
		}
		$dtotal = 0;
		if($xelanhdao['dau'])
		{
			foreach($xelanhdao['dau'] as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['name']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, "");
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Lít");
				$this->exzel->getActiveSheet()->setCellValue('F'.$row, $item['total']);
				$dtotal = $dtotal + $item['total'];
				$row++;
				$stt++;
			}
		}
		
		//$xtotal = '0';//$xelanhdao['xang'] + $xehuanluyen['xang'] + $banle['xang']+$capxangcatcohc['xang']+$capxangcatcokhoaxe['xang'];
		//$dtotal ='0';//$xelanhdao['xang'] + $xehuanluyen['dau'] + $banle['dau']+$capxangcatcohc['dau']+$capxangcatcokhoaxe['dau'];
		
		$row++;
		// Cắt cỏ khoa xe
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "*");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tổng");
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($xtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('F'.$row, number_format($dtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('G'.$row, "");
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	private function downloadreportbanle()
	{
		$filename = "bao-cao-ban-le-xang-dau";
		$title1 ="BÁO CÁO BÁN LẺ XĂNG DẦU";
		$tentruong = "TRƯỜNG CAO ĐẲNG NGHỀ SỐ 19";
		$tendonvi = "BỘ PHẬN XĂNG DẦU";
		$head1 = "CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM";
		$head2 = "Độc lập - Tự do - Hạnh phúc";
		$strdate = "Thái Bình, ngày ....... tháng ...... năm .....";
		$title2 = "V/v cấp phát xăng, dầu phục vụ đào tạo và các hoạt động của nhà trường (từ ngày.... đến ngày .... năm)";
		
			
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(25);		
		$this->exzel->getActiveSheet()->setCellValue('A1', $tentruong);
		$this->exzel->getActiveSheet()->mergeCells('A1:B1');
		$this->exzel->getActiveSheet()->mergeCells('A2:B2');
		$this->exzel->getActiveSheet()->mergeCells('C1:F1');
		$this->exzel->getActiveSheet()->mergeCells('C2:F2');
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->exzel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->exzel->getActiveSheet()->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(false);
		
		$this->exzel->getActiveSheet()->setCellValue('A2', $tendonvi);
		
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C1', $head1);
		
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);
		
		$this->exzel->getActiveSheet()->setCellValue('C2', $head2);
		
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setSize(14);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('C2')->getFont()->setBold(true);
		
		
		// Lấy dữ liệu
		$xelanhdao = $this->xangdau->get_ps_theo_banle();
		
		
		// Xử lý mảng
		$row = 8;
		// Tiêu đề của bảng dữ liệu

		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Ngày tháng");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Số lượng");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Đơn giá (VNĐ)");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Thành tiền (VNĐ)");
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, "Ghi chú");
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('B'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(true);
		$this->exzel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(true);
		$row++;		
		$this->exzel->getActiveSheet()->mergeCells('A9:E9');
		$this->exzel->getActiveSheet()->setCellValue('A9', "Xăng");
		$this->exzel->getActiveSheet()->getStyle('A9')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$row++;$stt=1;
		$xtotal =0;
		$xamount = 0;
		//var_dump($xelanhdao['xang']); exit();
		if($xelanhdao['xang'])
		{
			foreach($xelanhdao['xang'] as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, date("d/m/Y",strtotime($item['created_date'])));
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['total']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, $item['price']);
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, $item['amount']);
				$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
				$xtotal = $xtotal + $item['total'];
				$xamount = $xamount + $item['amount'];
				$row++;
			}
		}
		
		$row++;
		// Tổng xăng
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Tổng");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, number_format($xtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($xamount,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('F'.$row, "");
	
		
		$row++;		
		$this->exzel->getActiveSheet()->mergeCells('A'.$row.':E'.$row);
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Dầu");
		$this->exzel->getActiveSheet()->getStyle('A'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		$dtotal = 0;
		$damount =0;   
		if($xelanhdao['dau'])
		{
			foreach($xelanhdao['dau'] as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, date("d/m/Y",strtotime($item['created_date'])));
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['total']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, $item['price']);
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, $item['amount']);
				$this->exzel->getActiveSheet()->setCellValue('E'.$row, "");
				$dtotal = $dtotal + $item['total'];
				$damount = $damount + $item['amount'];
				$row++;
				$stt++;
			}
		}
		
		//$xtotal = '0';//$xelanhdao['xang'] + $xehuanluyen['xang'] + $banle['xang']+$capxangcatcohc['xang']+$capxangcatcokhoaxe['xang'];
		//$dtotal ='0';//$xelanhdao['xang'] + $xehuanluyen['dau'] + $banle['dau']+$capxangcatcohc['dau']+$capxangcatcokhoaxe['dau'];
		
		$row++;
		// Tổng dầu
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Tổng");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, number_format($dtotal,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($damount,2,'.',','));
		$this->exzel->getActiveSheet()->setCellValue('F'.$row, "");
		
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	// Trung tâm báo cáo
	public function reportcenter($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('bdate');
			$edate = $this->input->post('edate');
			if($bdate == '' || $edate == '')
			{
				$listdata	=	$this->xangdau->get_report_tong_hop_xe();
			}
			else
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata	=	$this->xangdau->get_report_thcat($duration);
			}
			$return = '';
			if($listdata)
			{
				
				$return = $this->generate_rp_thcat($listdata);
			}
			echo json_encode(array('return_data'=>$return));
					
			/*
			if($bdate == '' || $edate == '')
			{
				$listdata	=	$this->xangdau->get_report_tong_hop_xe();
			}
			else
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata	=	$this->xangdau->get_report_tong_hop_xe($duration);
			}
			if($listdata)
			{
				$return = '';
				$return = $this->generate_report($listdata);
			}
			echo json_encode(array('return_data'=>$return));
			*/
		}
		else {
			$data['ItemID'] = 'User';
			$data['title'] = 'Báo cáo tổng hợp theo xe';
			$data['description'] = 'Báo cáo tổng hợp theo xe';
			//$data['listdata']	=	$this->xangdau->get_report_tong_hop_xe($duration);
			
			$this->form_validation->set_rules('soluong', 'Số lượng', '');
			$this->form_validation->set_error_delimiters('', '<br />');
			
			$this->_template['page']	= 'users/xangdau/rpcenter';
			$this->system_library->load($this->_template['page'], $data);
		}
	}
	
	// Report tổng hợp theo xe
	
	public function report_xe($duration=null)
	{
		
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('bdate');
			$edate = $this->input->post('edate');
			if($bdate == '' || $edate == '')
			{
				$listdata	=	$this->xangdau->get_report_tong_hop_xe();
			}
			else
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$listdata	=	$this->xangdau->get_report_tong_hop_xe($duration);
			}
			if($listdata)
			{
				$return = '';
				$return = $this->generate_report($listdata);
			}
			echo json_encode(array('return_data'=>$return));
		}
		else {
			$data['ItemID'] = 'User';
			$data['title'] = 'Báo cáo tổng hợp theo xe';
			$data['description'] = 'Báo cáo tổng hợp theo xe';
			$data['listdata']	=	$this->xangdau->get_report_tong_hop_xe($duration);
			
			$this->form_validation->set_rules('soluong', 'Số lượng', '');
			$this->form_validation->set_error_delimiters('', '<br />');
			
			$this->_template['page']	= 'users/xangdau/rptonghopxe';
			$this->system_library->load($this->_template['page'], $data);
		}
	}
	
	public function report_week()
	{
	
		
			$data['ItemID'] = 'User';
			$data['title'] = 'Báo cáo tổng hợp theo xe';
			$data['description'] = 'Báo cáo tổng hợp theo xe';
				
			$this->form_validation->set_rules('soluong', 'Số lượng', '');
			$this->form_validation->set_error_delimiters('', '<br />');
				
			$this->_template['page']	= 'users/xangdau/rpweek';
			$this->system_library->load($this->_template['page'], $data);
		
	}
	
	public function xuat_report_xe_week($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('begin_date');
			$edate = $this->input->post('end_date');
			if($bdate == '' || $edate == '')
			{
				$listdata	=	$this->xangdau->get_report_xe_week();
				$filename = 'xuat_nhap_theo_tuan'.date('d-m-Y');
				$title = 'Báo cáo theo tuần '.date('d-m-Y');
			}
			else
			{
				$duration = $duration.'_'.$bdate.'_'.$edate;
				$filename = 'xuat_nhap_theo_tuan_tu_ngay_'.$bdate.'_den_ngay_'.$edate;
				$title ='Báo cáo theo tuần từ ngày '.$bdate.' đến ngày '.$edate;
				$listdata	=	$this->xangdau->get_report_xe_week($duration);
			}

			if($listdata)
			{
				$return = '';
				$return = $this->generate_excel_week($listdata,$filename,$title);
			}
				
		}
	}
	
	public function generate_excel_week($data,$filename,$title)
{
		//echo $this->input->post('idlop'); echo $idlop;
		$idlop = $this->input->post('idlop');
		$arrT = array();
		$arrT[1] = 'A';
		$arrT[2] = 'B';
		$arrT[3] = 'C';
		$arrT[4] = 'D';
		$arrT[5] = 'E';
		$arrT[6] = 'F';
		$arrT[7] = 'G';
		$arrT[8] = 'H';
		$arrT[9] = 'I';
		$arrT[10] = 'J';
		$arrT[11] = 'K';
		$arrT[12] = 'L';
		$arrT[13] = 'M';
		$arrT[14] = 'N';
		$arrT[15] = 'O';
		$arrT[16] = 'P';
		$arrT[17] = 'Q';
		$arrT[18] = 'R';
		$arrT[19] = 'S';
		$arrT[20] = 'T';
		$arrT[21] = 'U';
		$arrT[22] = 'V';
		$arrT[23] = 'W';
		$arrT[24] = 'X';
		$arrT[25] = 'Y';
		$arrT[26] = 'Z';
		$arrT[27] = 'AA';
		$arrT[28] = 'AB';
		$arrT[29] = 'AC';
		$arrT[30] = 'AD';
		$arrT[31] = 'AE';
		$arrT[32] = 'AF';
		$arrT[33] = 'AG';
		$arrT[34] = 'AH';
		$arrT[35] = 'AI';
		$arrT[36] = 'AJ';
		$arrT[37] = 'AK';
		$arrT[38] = 'AL';
		$arrT[39] = 'AM';
		$arrT[40] = 'AN';
		$arrT[41] = 'AO';
		$arrT[42] = 'AP';
		$arrT[43] = 'AQ';
		$arrT[44] = 'AR';
		$arrT[45] = 'AS';
		$arrT[46] = 'AT';
		$arrT[47] = 'AU';
		$arrT[48] = 'AV';
		$arrT[49] = 'AW';
		$arrT[50] = 'AX';
		$arrT[51] = 'AY';
		
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bang Diem');
		//set cell A1 content with some text
		
			$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
			$this->exzel->getActiveSheet()->setCellValue('A7', 'TT');
			$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
			$this->exzel->getActiveSheet()->setCellValue('B7', 'Biển KS');
			$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
			$this->exzel->getActiveSheet()->setCellValue('C7', 'Giáo viên');
			$this->exzel->getActiveSheet()->setCellValue('D7', 'Mã GV');
			$this->exzel->getActiveSheet()->setCellValue('E7', 'Bảng quyết toán');
			//change the font size
			$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setSize(14);
			$this->exzel->getActiveSheet()->getStyle('E7')->getFont()->setSize(18);
			//make the font become bold
			$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setBold(false);
			//merge cell A1 until D1
			$this->exzel->getActiveSheet()->mergeCells('A7:A9');
			//set aligment to center for that merged cell (A1 to D1)
			$this->exzel->getActiveSheet()->getStyle('A7')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
			$this->exzel->getActiveSheet()->getStyle('E7')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
			$this->exzel->getActiveSheet()->mergeCells('B7:B9');
			$this->exzel->getActiveSheet()->mergeCells('C7:C9');
			$this->exzel->getActiveSheet()->mergeCells('D7:D9');
		
			$idex = 4;
			$jdex = 5;
			
			// Start date
			$date = '2014-10-18';
			// End date
			$end_date = '2014-10-24';
			
			$datearr = array();
			while (strtotime($date) <= strtotime($end_date)) {
				$datearr[] = $date;
				$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
				
			}
			
			foreach($datearr as $dates)
			{
				$idex++;
			}
			
			$mergeCell =  'E7:'.$arrT[$idex].'7';
			$this->exzel->getActiveSheet()->mergeCells($mergeCell);
			foreach($datearr as $dates)
			{
				$colname = $arrT[$jdex].'8';
				//echo $colname; exit();
				$this->exzel->getActiveSheet()->getColumnDimension($arrT[$jdex])->setWidth(5);
				$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].'8', $dates);
				$colname = $arrT[$jdex].'9';
				//echo $colname; exit();
				$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].'9', $dates);
				$jdex++;
			}
			//NEXT COLUM
			$idex++;
			$mergeCell = $arrT[$idex].'7'.':'.$arrT[$idex].'9';
			$this->exzel->getActiveSheet()->mergeCells($mergeCell);
			
			$this->exzel->getActiveSheet()->getStyle($arrT[$idex].'7')->getAlignment()->setTextRotation(90);
			$this->exzel->getActiveSheet()->getColumnDimension($arrT[$idex])->setWidth(5);
			$this->exzel->getActiveSheet()->setCellValue($arrT[$idex].'7', 'ĐTB');
			$jdex = $idex + 1;
			//var_dump($data['exams']); exit();
			/* foreach ($data['exams'] as $exam)
			{
				
				$mergeCell = $arrT[$jdex].'7'.':'.$arrT[$jdex].'9';
				$this->exzel->getActiveSheet()->mergeCells($mergeCell);
				//echo $colname; exit();
				$this->exzel->getActiveSheet()->getStyle($arrT[$jdex].'7')->getAlignment()->setTextRotation(90);
				$this->exzel->getActiveSheet()->getColumnDimension($arrT[$jdex])->setWidth(5);
				$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].'7', $exam['mamon']);
				$jdex++;
				
			}
			$idex = $jdex; */
			//echo $idex;
			$mergeCell = $arrT[$idex].'7'.':'.$arrT[$idex].'9';
			$this->exzel->getActiveSheet()->mergeCells($mergeCell);
			$this->exzel->getActiveSheet()->getStyle($arrT[$idex].'7')->getAlignment()->setTextRotation(90);
			$this->exzel->getActiveSheet()->getColumnDimension($arrT[$idex])->setWidth(5);
			$this->exzel->getActiveSheet()->setCellValue($arrT[$idex].'7', 'Tổng DC');
			
			$idex++;
			$mergeCell = $arrT[$idex].'7'.':'.$arrT[$idex].'9';
			$this->exzel->getActiveSheet()->mergeCells($mergeCell);
			$this->exzel->getActiveSheet()->getStyle($arrT[$idex].'7')->getAlignment()->setTextRotation(90);
			$this->exzel->getActiveSheet()->getColumnDimension($arrT[$idex])->setWidth(5);
			$this->exzel->getActiveSheet()->setCellValue($arrT[$idex].'7', 'Thực cấp');
		
			// INSERT DIEM
			$stt = 1;
			$irow = 10;
			
			foreach($data as $item)
			{ 
				//echo $item['name'].'<br />'; exit();
			
					$name = $item['name'];
					//echo $name; exit();
					$this->exzel->getActiveSheet()->setCellValue('A'.$irow, $stt);
					$this->exzel->getActiveSheet()->setCellValue('B'.$irow, $item['name']);
					$this->exzel->getActiveSheet()->setCellValue('C'.$irow, '');
					
					$this->exzel->getActiveSheet()->setCellValue('D'.$irow, '');
					$jdex = 5;
					//$dachi = $item['dachi'];
					
					
					
					foreach($datearr as $ngay)
					{
						//echo $item['id'].$ngay;
						$get_total = $this->xangdau->get_report_xe_theo_tuan($item['id'], $ngay);
						if($get_total == null)
						{
							$dachi = 0;
						}
						else {
							$dachi = $get_total->ps_giam;
						}
						$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].$irow, $dachi);
						$jdex++;
					}
					//$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].$irow, $dtb);
					
					$jdex++;
					
					/*
					foreach ($data['exams'] as $exam)
					{
						//echo $colname; exit();
						$point = $this->basic_library->get_total_mark_of_students_by_class($hocvien['id'],$exam['id']);
						if($point)
						{
						$mark = $point->diemtrungbinh;
						}
						else {
							$mark = "Chưa nhập điểm";
						}
						$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].$irow, $mark);
						$jdex++;
							
					}
					$dtk = round($hocvien['dtk'],1);
					$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].$irow, $dtk);
					$jdex++;
					$xl = $hocvien['xeploai'];
					$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].$irow, $xl); */
					//$jdex++;
					//$this->exzel->getActiveSheet()->setCellValue($arrT[$jdex].$irow, $hocvien['kind']);
					$irow++;
					$stt++;
				
			}
		
		
		$filename='bao_cao_xe_theo_tuan.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		 
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
		////////////////////

	}
	
	
	public function generate_report($array)
	{
		$html = '';
		//var_dump($array);
		foreach ($array as $item)
		{
			$temp = $item['chungtus'];
			$html = $html. "<br />" . "<p><strong>".$item['name']."</strong></p><br/>";
			
			$html = $html. '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">';
			$html = $html. '<thead>
				<tr>
					<th>STT</th>
					<th>Hạng mục</th>
					<th>Nhận (Lít)</th>					
					<th>Nhận (Đ)</th>
	
				</tr>
			</thead>
			<tbody>';
			$stt = 1;
			
					foreach($temp as $data):
				
					$html = $html. '<tr id="item_'. $data['id']. '">';
					$html = $html. '<td>'.$stt.'</td>';
					$html = $html. '<td id="name_item_'.$data['id'].'"><a href="'. site_url().'member/xangdau/report_detail_by_theitem/'.$data['tbid'].'">'. $data['tbname'] .'</a></td>';
					$html = $html. '<td id="ps_giam_'. $data['id'].'" class="ta-right">';
					$html = $html.  number_format($data['total'],2,'.',',');
					$html = $html. '</td>';
					$html = $html. '<td class="ta-right">'. number_format($data['amount'],0,'.',',').'</td>';
	
				$html = $html. '</tr>';
				$stt++;
				 endforeach;
			$html = $html.'</tbody>
			
		</table>
		<br/>';
				
		}
		return $html;
	}
	
	// tạo report tong hop
	public function tao_bc_tong_hop()
	{
		$id = $this->input->post('id');
		$return_id = $this->xangdau->tao_bc_tong_hop();
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	

	// View Báo cáo tổng hợp
	public function report_tong_hop($duration=null)
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Báo cáo Xăng dầu tổng hợp';
		$data['description'] = 'Báo cáo Xăng dầu tổng hợp';
		$data['listdata']	=	$this->xangdau->get_report_tong_hop($duration);
		$data['prelistdata']= $this->xangdau->get_report_tong_hop_thang_truoc();
		
		$this->form_validation->set_rules('soluong', 'Số lượng', '');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$this->_template['page']	= 'users/xangdau/rptonghop';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	// Report Report chi tiết
	
	public function report_chitiet($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$type = $this->input->post('type');
			$bdate = $this->input->post('bdate');
			$edate = $this->input->post('edate');
			switch ($type)
			{
				case "today":
					$listdata = $this->xangdau->get_all_chung_tu_ok('TODAY');
					break;
				case "thismonth":
					$listdata = $this->xangdau->get_all_chung_tu_ok('THISMONTH');
					break;
				case "lastmonth":
					$listdata = $this->xangdau->get_all_chung_tu_ok('LASTMONTH');
					break;
				case "customize":
					if($bdate == '' || $edate == '')
					{
						$listdata	=	$this->xangdau->get_all_chung_tu_ok('THISMONTH');
					}
					else
					{
						$duration = $duration.'_'.$bdate.'_'.$edate;
						$listdata	=	$this->xangdau->get_all_chung_tu_ok($duration);
					}
					break;
			}
			
			if($listdata)
			{
				$return = '';
				$return = $this->generate_report_chi_tiet($listdata);
			}
			echo json_encode(array('return_data'=>$return));
		}
		else {
			$data['ItemID'] = 'User';
			$data['title'] = 'Báo cáo Xăng dầu chi tiết';
			$data['description'] = 'Báo cáo Xăng dầu chi tiết';
			$this->load->helper('form');
			
			$data['listdata']	=	$this->xangdau->get_all_chung_tu_ok($duration);
			//$data['tong'] = $this->xangdau->tinh_tong_ps($duration);
			$data['thanhtien'] = $this->xangdau->tinh_tong_ps_thanh_tien($duration);
			$this->form_validation->set_rules('soluong', 'Số lượng', '');
			$this->form_validation->set_error_delimiters('', '<br />');
			
			$this->_template['page']	= 'users/xangdau/rpchitiet';
			$this->system_library->load($this->_template['page'], $data);
		}
	}
	
	function generate_report_chi_tiet($listdata)
	{
		$html = '';
		
		$html = '<br/>
		<table class="table" cellpadding="0" cellspacing="0">';
		$html = $html . '<tr><td>Tên hàng</td><td>Phát sinh tăng</td><td>Phát sinh giảm</td><td>Thành tiền PS tăng</td><td>Thành tiền PS giảm</td></tr>';
		foreach($listdata as $item):
			$tg = $item['tong'];
			$tgt = $item['tongtt'];
			
			$html = $html . '<tr>';
			$html = $html . '<td>'.$item['name'].'</td>';
			$html = $html . '<td>'.number_format($tg[0]['tang'],2,'.',',') .' Lít</td>';
			$html = $html . '<td>'.number_format($tg[0]['giam'],2,'.',',').' Lít</td>';
			$html = $html . '<td>'.number_format($tgt[0]['tang'],0,'.',',').' đ</td>';
			$html = $html . '<td>'.number_format($tgt[0]['giam'],0,'.',',').' đ</td>';
			$html = $html . '</tr>';
		endforeach;
			$html = $html . '</table>
			<br/>';
		
			foreach($listdata as $item):
				$tong = $item['tong'];
				$html = $html . '<div>Chi tiết: <strong>'. $item['name'].'</strong></div>';
				$html = $html . '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">
					<thead>
						<tr>
							<th>Ngày tháng</th>
							<th>Đơn vị nhận</th>
							<th>Phát sinh tăng</th>
							<th>Phát sinh giảm</th>
							<th>Đơn giá</th>
							<th >
								<span id="tip" class="functions text-center">Thành tiền</span>
							</th>
			
						</tr>
					</thead>';
					
				$html = $html . '<tbody>	
						<tr class="sum_f">
							<td>Tổng</td>
							<td></td>';
				$html = $html . '<td class="ta-right">'. number_format($tong[0]['tang'],2,'.',',').'</td>';
				$html = $html . '<td class="ta-right">'. number_format($tong[0]['giam'],2,'.',',').'</td>';
				$html = $html . '<td></td>
							<td></td>
						</tr>';
				$stt = 1;// +1; 
						
				foreach($item['chungtus'] as $data):
				$html = $html . '<tr id="item_'. $data['id'].'">';
				$html = $html . '<td>'. date('d/m/Y',strtotime($data['created_date'])).'</td>';
				$html = $html . '<td id="name_thiet_bi_'. $data['id'].'">';
				if($data['tbname'] != null)
				{
					$html = $html . $data['tbname']; 
				} 
				else 
				{ 
					$html = $html . 'Nhập kho';
				}
				$html = $html . '</td>';
				$html = $html . '<td id="ps_tang_'. $data['id'].'" class="ta-right">';
				$html = $html .  number_format($data['ps_tang'],2,'.',',');
				$html = $html . '</td>';
				$html = $html . '<td id="ps_giam_'. $data['id'].'" class="ta-right">';
				$html = $html . number_format($data['ps_giam'],2,'.',',');
				$html = $html . '</td>';
				$html = $html . '<td id="name_name_'. $data['id'].'">'. number_format($data['price'],0,'.',',') .'</td>';
				$html = $html . '<td class="functions">';
								
						
				$html = $html . '<p id="dpl_'. $data['id'].'">'. number_format($data['price']*($data['ps_giam'] + $data['ps_tang']),0,'.',',').'</p>';
				$html = $html . '</td>
			
						</tr>';
						endforeach;
				$html = $html . '</tbody>';
					
				$html = $html . '</table>
				<br/>';
				endforeach;
		return $html;
	}
	// Delete chứng từ
	public function delete_chung_tu()
	{
		$id = $this->input->post('id');
		$return_id = $this->xangdau->delete_chung_tu($id);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	// Submit chứng từ
	public function submit_chung_tu()
	{
		$id = $this->input->post('id');
		$return_id = $this->xangdau->submit_chung_tu($id);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	// Edit chứng từ
	public function save_chung_tu()
	{
		$id = $this->input->post('id');
		$ps_tang = $this->input->post('ps_tang');
		$ps_giam = $this->input->post('ps_giam');
		
		$return_id = $this->xangdau->save_chung_tu($id,$ps_tang,$ps_giam);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	//Submit thiết bị
	public function submit_thiet_bi()
	{
		$id = $this->input->post('id');
		$return_id = $this->xangdau->submit_thiet_bi($id);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	// submit hạng mục
	public function submit_hang_muc()
	{
		$id = $this->input->post('id');
		$return_id = $this->xangdau->submit_hang_muc($id);
		if($return_id)
		{
			$status = 1;
			// Insert so du dau ky 0
			$this->xangdau->add_sddk($id,0);
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	public function submit_sddk()
	{
		
	}
	// Thêm hạng mục
	public function them_hang_muc()
	{
		$name = $this->input->post('name');
		$description = $this->input->post('description');
		
			$return_id = $this->xangdau->add_category($name,$description);
			if($return_id)
			{
				$status = 1;
			}
			else
			{
				$status = 0;
			}
		
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	// Thêm thiết bị
	public function them_thiet_bi()
	{
		$name = $this->input->post('name');
		$code = $this->input->post('code');
		$description = $this->input->post('description');
		$type = $this->input->post('type');
		$category  = $this->input->post('category');
		if($this->xangdau->getthietbibycode($code))
		{
			$return_id = -1;
			$status = 3;
		}
		else {
			$return_id = $this->xangdau->themthietbi($name,$code,$description,$type,$category);
			if($return_id)
			{
				$status = 1;
			}
			else
			{
				$status = 0;
			}
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	// Thêm chứng từ
	public function them_chung_tu()
	{

		$hang_muc_id = $this->input->post('hang_muc_id');
		$ps_tang = $this->input->post('ps_tang');
		$ps_giam = $this->input->post('ps_giam');
		$note = $this->input->post('note');
		$price = $this->input->post('price');
		$thiet_bi_id = $this->input->post('thiet_bi_id');
		$return_id = $this->xangdau->themchungtu($hang_muc_id,$ps_tang,$ps_giam,$note,$price,$thiet_bi_id);
		$item = $this->xangdau->get_dvt($hang_muc_id);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id, 'dvt'=>$item[0]['dvt']));
	}
	
	public function submitsddk()
	{
		$id = $this->input->post('id');
		if($this->taichinh->submitsddk($id))
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status));
	}
	public function saveiteminstore()
	{
		$id = $this->input->post('id');
		$amount= $this->input->post('amount');
		if($this->taichinh->saveiteminstore($id,$amount))
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status));
	}
	//Lits
	public function viewsodudauky()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Số dư đầu kỳ';
		$data['description'] = 'Số dư đầu kỳ';
		$this->load->helper('form');
		
		$data['listdata']	=	$this->xangdau->get_all_sddk();
		
		$this->form_validation->set_rules('soluong', 'Số lượng', '');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$this->_template['page']	= 'users/xangdau/chitietdauky';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	// List hang muc
	function listhangmuc()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Hạng mục cho Xăng dầu';
		$data['description'] = 'Nhập hạng mục cho Xăng dầu tại đây';
		$this->load->helper('form');
		
		$data['listdata']	=	$this->xangdau->get_all_category();
		$this->form_validation->set_rules('soluong', 'Số lượng', '');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$this->_template['page']	= 'users/xangdau/listhangmuc';
		$this->system_library->load($this->_template['page'], $data);
	}
	// List thiet bi
	function listthietbi()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Thiết bị';
		$data['description'] = 'Danh sách Thiết bị';
		$this->load->helper('form');
		
		$data['listdata']	=	$this->xangdau->get_all_thietbi();
		
		
		
		$this->form_validation->set_rules('soluong', 'Số lượng', '');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$this->_template['page']	= 'users/xangdau/listthietbi';
		$this->system_library->load($this->_template['page'], $data);
	}
	function listchungtu()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Nhập chứng từ xuất nhập xăng tại đây';
		$data['description'] = 'Nhập chứng từ xuất nhập';
		$this->load->helper('form');
		
		$data['listdata']	=	$this->xangdau->get_all_chung_tu();
		$data['listsource']	=	$this->xangdau->get_report_tong_hop();
		//var_dump($data['listdata']);
		$listcates = $this->xangdau->get_all_category(1);
		
		$listtbs = $this->xangdau->get_all_thietbi(1);
		$drop[0] = "Chọn...";
		if($listcates != null)
		{
			foreach($listcates as $cate)
			{
				$drop[$cate['id']] = $cate['name'];
			}
		}
		else {
			$drop = null;
		}
		
		$tb[0] = 'Nhập kho';
		if($listtbs != null)
		{
			foreach($listtbs as $cate)
			{
				$tb[$cate['id']] = $cate['name'];
			}
		}
		else {
			$tb = null;
		}
		//var_dump($drop);
		$xang[] = array('id'=>'0','name'=>'Nhập kho','code'=>'Nhập kho');
		$dau[] = array('id'=>'0','name'=>'Nhập kho','code'=>'Nhập kho');
		$xangdau[] = array('id'=>'0','name'=>'Nhập kho','code'=>'Nhập kho');
		$rsxang = $this->xangdau->get_all_thietbi(1,1);
		$rsdau = $this->xangdau->get_all_thietbi(1,2);
		//var_dump($rsdau);
		if($rsdau)
		{
			foreach($rsdau as $item)
			{
				$dau[] = array('id'=>$item['id'],'name'=>$item['name'],'code'=>$item['code']);
			}
		}
		if($rsxang)
		{
			foreach($rsxang as $item)
			{
				$xang[] = array('id'=>$item['id'],'name'=>$item['name'],'code'=>$item['code']);
			}
		}
		if($listtbs != null)
		{
			foreach($listtbs as $item)
			{
				$xangdau[] = array('id'=>$item['id'],'name'=>$item['name'],'code'=>$item['code']);
			}
		}
		$data['xang'] = $xang;
		$data['dau'] = $dau;
		$data['xangdau'] = $xangdau;
		$data['drop'] = $drop;
		$data['tb'] = $tb;
		$this->form_validation->set_rules('soluong', 'Số lượng', '');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$this->_template['page']	= 'users/xangdau/listchungtu';
		$this->system_library->load($this->_template['page'], $data);
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/teacher.php */